package com.googlecode.jlogindexer.client;

import java.io.File;

import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListener;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.BeanFactory;

import com.googlecode.jlogindexer.client.utils.SpringUtils;

public class JLogalizerClient {
	
	BeanFactory context=SpringUtils.getContext();

	public void startTailing() throws InterruptedException{
		
		//FIXME Pseudocode - to be implemented later
		/*1. File configuration to be read from XML.
		 *2. XML to store unique id of the client, hostname and filepath
		 *3. Read each configuration (inside configurations tag) and construct thread   
		 */
		
		/*List<Configuration> configurations = configBuilder.getConfigurations().getConfiguration();
		for (Configuration configuration : configurations) {
			
			startThreads(xxx, configuration);
			
		}*/
		startThreads();
		//this will help the client not to exit
		/*Object obj=new Object();
		synchronized(obj){
			obj.wait();
		}*/
	}

	
	//private void startThreads(xxxx, Configuration configuration) {
		
	private void startThreads(){
		String hostname="127.0.0.1";
		String filePath="d:/logs/server.log";
		String clientId="12345ABCDE";
		
		String threadName=StringUtils.trimToEmpty(clientId+"_"+hostname+"_"+filePath);
		
		
		TailerListener listener=(JLogalizerTailerListener)context.getBean("jlogalizerTailerListener", clientId, hostname, filePath);
		
		//TailerListener listener = new JLogalizerTailerListener(hostname, filePath);
		Tailer tailer = new Tailer(new File(filePath), listener, 1000, true);
		System.out.println(tailer.getFile().getAbsolutePath());
		System.out.println(tailer.getDelay());
	    Thread thread = new Thread(tailer, threadName);
	    thread.start();
		
	}
	
	
	
}

